package com.retail.statistics.client.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.retail.core.entity.result.Result;
import com.retail.order.entity.vo.StatisticsOrderDetailVo;
import com.retail.order.service.RetailSettleOrderService;
import com.retail.shop.service.RetailShopService;
import com.retail.statistics.client.DashboardStatisticsClient;
import com.retail.order.entity.vo.LabelValueVo;
import com.retail.order.entity.vo.NameValueVo;
import com.retail.system.enums.UserTypeEnums;
import com.retail.system.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
public class DashboardStatisticsClientImpl implements DashboardStatisticsClient {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RetailShopService retailShopService;

    @Autowired
    private RetailSettleOrderService retailSettleOrderService;

    @Override
    public Result fullStatistics(Long adminId, Integer userType) {

        List<LabelValueVo> res = new ArrayList<>();

        if (ObjectUtil.equal(UserTypeEnums.SYSTEM_MANAGER_TYPE.getValue(), userType)) {
            fullStatisticsManager(res);
        } else if (ObjectUtil.equal(UserTypeEnums.BUSINESS_TYPE.getValue(), userType)) {
            // 团队长
            Map<String, Object> orderStatisticsFull = retailSettleOrderService.orderFullStatisticsByTuanduizId(adminId);
            res.add(LabelValueVo.builder().label("团队成员数").value(orderStatisticsFull.get("tuandui_person_count") + "").build());
            res.add(LabelValueVo.builder().label("商户数").value(orderStatisticsFull.get("shop_count") + "").build());
            res.add(LabelValueVo.builder().label("订单总数/总金额").value(orderStatisticsFull.get("order_count") + "/" + orderStatisticsFull.get("order_price")).build());
        } else if (ObjectUtil.equal(UserTypeEnums.SHOP_TYPE.getValue(), userType)) {
            // 商户数据
            Map<String, Object> orderStatisticsFull = retailSettleOrderService.orderFullStatisticsByAdminId(adminId);
            res.add(LabelValueVo.builder().label("当日订单/金额").value(orderStatisticsFull.get("day_count") + "/" + orderStatisticsFull.get("day_price")).build());
            res.add(LabelValueVo.builder().label("当月订单/金额").value(orderStatisticsFull.get("month_count") + "/" + orderStatisticsFull.get("month_price")).build());
            res.add(LabelValueVo.builder().label("本年订单/金额").value(orderStatisticsFull.get("year_count") + "/" + orderStatisticsFull.get("year_price")).build());
        }

        return Result.success("查询成功！", res);
    }

    /**
     * @param adminId  用户编号
     * @param userType 用户类型
     * @param dateType 数据类型
     * @return
     */
    @Override
    public Result orderFullStatistics(Long adminId, Integer userType, String dateType) {

        String startTime = "";
        String endTime = "";
        if (ObjectUtil.equal(dateType, "0")) {
            startTime = DateUtil.format(new Date(), "yyyy-MM-01");
            endTime = DateUtil.format(DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, 1), "yyyy-MM-dd");
        } else if (ObjectUtil.equal(dateType, "1")) {
            startTime = DateUtil.format(new Date(), "yyyy-01-01");
            endTime = DateUtil.format(DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, 1), "yyyy-MM-dd");
        }

        List<StatisticsOrderDetailVo> list = retailSettleOrderService.orderFullStatistics(adminId, userType, startTime, endTime, dateType);
        Map<String, Object> res = new HashMap<>();
        List<String> rq = new ArrayList<>();
        List<Integer> orderCount = new ArrayList<>();
        List<BigDecimal> orderPrice = new ArrayList<>();
        list.forEach(v -> {
            rq.add(v.getRq());
            orderCount.add(v.getOrderCount());
            orderPrice.add(v.getOrderPrice());
        });
        res.put("rq", rq);
        res.put("orderCount", orderCount);
        res.put("orderPrice", orderPrice);
//        return Result.success(list);
        return Result.success(res);
    }

    @Override
    public Result orderShopStatistics(Long adminId, Integer userType, String dateType) {
        String startTime = "";
        String endTime = "";
        if (ObjectUtil.equal(dateType, "0")) {
            startTime = DateUtil.format(new Date(), "yyyy-MM-01");
            endTime = DateUtil.format(DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, 1), "yyyy-MM-dd");
        } else if (ObjectUtil.equal(dateType, "1")) {
            startTime = DateUtil.format(new Date(), "yyyy-01-01");
            endTime = DateUtil.format(DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, 1), "yyyy-MM-dd");
        }
        List<NameValueVo> list = retailSettleOrderService.orderShopStatistics(adminId, userType, startTime, endTime, dateType);
        return Result.success(list);
    }

    @Override
    public Result ordershopListFullStatistics(Long adminId, Integer userType, String dateType) {
        String startTime = "";
        String endTime = "";
        if (ObjectUtil.equal(dateType, "0")) {
            startTime = DateUtil.format(new Date(), "yyyy-MM-01");
            endTime = DateUtil.format(DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, 1), "yyyy-MM-dd");
        } else if (ObjectUtil.equal(dateType, "1")) {
            startTime = DateUtil.format(new Date(), "yyyy-01-01");
            endTime = DateUtil.format(DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, 1), "yyyy-MM-dd");
        } else if (ObjectUtil.equal(dateType, "3")) {
            startTime = DateUtil.format(new Date(), "yyyy-MM-dd");
            endTime = DateUtil.format(DateUtil.offset(new Date(), DateField.DAY_OF_YEAR, 1), "yyyy-MM-dd");
        }

        List<StatisticsOrderDetailVo> list = retailSettleOrderService.ordershopListFullStatistics(adminId, userType, startTime, endTime, dateType);
        Map<String, Object> res = new HashMap<>();
        List<String> rq = new ArrayList<>();
        List<Integer> orderCount = new ArrayList<>();
        List<BigDecimal> orderPrice = new ArrayList<>();
        list.forEach(v -> {
            rq.add(v.getRq());
            orderCount.add(v.getOrderCount());
            orderPrice.add(v.getOrderPrice());
        });
        res.put("rq", rq);
        res.put("orderCount", orderCount);
        res.put("orderPrice", orderPrice);
//        return Result.success(list);
        return Result.success(res);
    }

    // 管理员数据统计
    private void fullStatisticsManager(List<LabelValueVo> res) {
        // 管理员
        Integer allYwyCount = adminService.allYwy();
        res.add(LabelValueVo.builder().label("区域合伙人数").value(allYwyCount + "").build());
        // 商户数
        Integer allShopCount = retailShopService.allShops();
        res.add(LabelValueVo.builder().label("商户数").value(allShopCount + "").build());
        // 订单数据
        Integer allOrderCount = retailSettleOrderService.allOrders();
        res.add(LabelValueVo.builder().label("核心订单数").value(allOrderCount + "").build());
    }
}
